package com.cyz.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import com.cyz.pojo.Paging;
import com.cyz.service.ReserveService;
import com.cyz.service.TypeService;
import com.google.gson.Gson;

/**
 * @author 陈雅珠
 * 预约管理
 */
@Controller
@RequestMapping("/ReserveManage")
public class ReserveManage {

	@Resource(name="mv")
	private ModelAndView mv;
	@Resource(name="typeServiceImpl")
	private TypeService typeService;
	@Resource(name="reserveServiceImpl")
	private ReserveService reserveService;
	@Resource(name="gson")
	private Gson gson;
	
	/**
	 * 预约列表页面
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping("/reserve-list")
	public ModelAndView reserveList(HttpServletRequest request,HttpServletResponse response) {
		mv.addObject("typeList",typeService.getAllType());
		mv.setViewName("admin/reserve-list");
		return mv;
	}
	
	/**
	 * 已取消的列表页面(用户取消)
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping("/reserve-cancel")
	public ModelAndView reserveCancel(HttpServletRequest request,HttpServletResponse response) {
		mv.addObject("typeList",typeService.getAllType());
		mv.setViewName("admin/reserve-cancel");
		return mv;
	}
	
	/**
	 * 已取消的列表页面（官方取消）
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping("/reserve-official")
	public ModelAndView reserveOfficial(HttpServletRequest request,HttpServletResponse response) {
		mv.addObject("typeList",typeService.getAllType());
		mv.setViewName("admin/reserve-official");
		return mv;
	}
	
	/**
	 * 已过期的预约页面
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping("/reserve-expired")
	public ModelAndView reserveExpired(HttpServletRequest request,HttpServletResponse response) {
		mv.addObject("typeList",typeService.getAllType());
		mv.setViewName("admin/reserve-expired");
		return mv;
	}
	
	/**
	 * 审核通过的预约页面
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping("/reserve-approve")
	public ModelAndView reserveApprove(HttpServletRequest request,HttpServletResponse response) {
		mv.addObject("typeList",typeService.getAllType());
		mv.setViewName("admin/reserve-approve");
		return mv;
	}
	
	/**
	 * 预约列表页面
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping("/reserve-update")
	public ModelAndView reserve_update(HttpServletRequest request,HttpServletResponse response) {
		
		
		mv.setViewName("admin/reserve-update");
		return mv;
	}
	
	/**
	 * 查询预约
	 * @param request
	 * @param response
	 * @throws IOException
	 */
	@RequestMapping("/getReserve")
	public void getReserve(HttpServletRequest request,HttpServletResponse response) throws IOException {
		response.setContentType("text/xml;charset=UTF-8");
	    response.setCharacterEncoding("UTF-8");
		PrintWriter printWriter = response.getWriter();
		Map map = new HashMap();
		map.put("state",Integer.parseInt(request.getParameter("state"))); //状态
		map.put("idNumber",request.getParameter("idNumber")); //身份证号码
		map.put("page",Integer.parseInt(request.getParameter("page"))); //当前页
		map.put("pageSize",Integer.parseInt(request.getParameter("pageSize"))); //页面大小
		map.put("startTime",request.getParameter("startTime")); //起始时间
		map.put("endTime",request.getParameter("endTime")); //结束时间
		map.put("subject",request.getParameter("subject"));
		map.put("typeName",request.getParameter("typeName"));
		Paging paging = reserveService.getReserve(map);
		String s = gson.toJson(paging);
		printWriter.write(s);
		printWriter.close();
	}
	
	/**
	 * 批量取消预约
	 * @throws IOException 
	 */
	@RequestMapping("/deleteReserveByQuery")
	public void deleteReserveByQuery(HttpServletRequest request,HttpServletResponse response) throws IOException {
		PrintWriter printWriter = response.getWriter();
		String list = request.getParameter("reserveIdList");
		String[] list1 = gson.fromJson(list,String[].class); //获取要删除的学生编号
		
		for(String reserveId:list1) { //循环遍历删除
			reserveService.deleteReserve(Integer.parseInt(reserveId));
		}
		printWriter.write("{\"res\":\"1\"}");
		printWriter.close();
	}
	
	/**
	 * 删除预约
	 * @param request
	 * @param response
	 * @throws IOException
	 */
	@RequestMapping("/deleteReserve")
	public void deleteReserve(HttpServletRequest request,HttpServletResponse response) throws IOException {
		PrintWriter printWriter = response.getWriter();
		Integer reserveId = Integer.parseInt(request.getParameter("reserveId"));
		reserveService.deleteReserve(reserveId);
		printWriter.write("{\"res\":\"1\"}");
		printWriter.close();
	}
	
	/**
	 * 审核通过预约
	 * @param request
	 * @param response
	 * @throws IOException
	 */
	@RequestMapping("/approveReserve")
	public void approveReserve(HttpServletRequest request,HttpServletResponse response) throws IOException {
		PrintWriter printWriter = response.getWriter();
		Integer reserveId = Integer.parseInt(request.getParameter("reserveId")); //要取消的预约编号
		reserveService.approveReserve(reserveId);
		printWriter.write("{\"res\":\"1\"}");
		printWriter.close();
	}
	
	/**
	 * 批量审核
	 * @throws IOException 
	 */
	@RequestMapping("/approveReserveByQuery")
	public void approveReserveByQuery(HttpServletRequest request,HttpServletResponse response) throws IOException {
		PrintWriter printWriter = response.getWriter();
		String list = request.getParameter("reserveIdList");
		String[] list1 = gson.fromJson(list,String[].class); //获取要删除的学生编号
		for(String reserveId:list1) { //循环遍历删除
			reserveService.approveReserve(Integer.parseInt(reserveId));
		}
		printWriter.write("{\"res\":\"1\"}");
		printWriter.close();
	}
	
}
